package com.wzxy.medical_backed.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzxy.medical_backed.entity.Policy;
import com.wzxy.medical_backed.entity.Reimbursement;
import com.wzxy.medical_backed.mapper.ReimbursementMapper;
import com.wzxy.medical_backed.service.DiseaseService;
import com.wzxy.medical_backed.service.PolicyService;
import com.wzxy.medical_backed.service.ReimbursementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Calendar;

@Service
public class ReimbursementServiceImpl extends ServiceImpl<ReimbursementMapper, Reimbursement>
        implements ReimbursementService {

    @Autowired
    private PolicyService policyService;

    @Autowired
    private DiseaseService diseaseService;

    @Override
    @Transactional
    public boolean updateReimbursementStatus(Long id, String status) {
        Reimbursement entity = new Reimbursement();
        entity.setId(id);
        entity.setIsReimbursement(status);
        return updateById(entity);
    }

    @Override
    @Transactional
    public boolean updateRemitStatus(Long id, String status) {
        Reimbursement entity = new Reimbursement();
        entity.setId(id);
        entity.setIsRemit(status);
        return updateById(entity);
    }

    @Override
    public Double calculateReimbursementAmount(Double totalCost, String medicalName, java.util.Date date) {
        String year = null;
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            year = String.valueOf(calendar.get(Calendar.YEAR));
        }
        Policy policy = policyService.lambdaQuery()
                .eq(year != null, Policy::getYear, year)
                .one();
        if (policy != null && policy.getPercentage() != null) {
            return totalCost * (policy.getPercentage() / 100.0);
        } else {
            return totalCost * 0.6;
        }
    }
}